<?php
/**
 * Zend Image Manipulation Module
 *
 * LICENSE
 *
 * This source file is subject to the new BSD license that is bundled
 * with this package in the file LICENSE.txt.

 * @category   Zimm
 * @package    Zimm
 * @copyright  Copyright (c) 2012 Kristoffer Ingemansson
 * @license    New BSD License
 * @version    $Id: Zimm.php 4 2012-04-14 12:59:16Z kristoffer.ingemansson@gmail.com $
 */

/**
 * Helper for generating urls from image path and instruction.
 */
class Zimm_View_Helper_Zimm extends Zend_View_Helper_Abstract
{
    /**
     * Generates an url from image path and instruction.
     *
     * @param  integer $instructionId
     * @param  string  $imagePath
     * @param  string  $customName OPTIONAL
     * @param  string  $customDir OPTIONAL
     * @return string  Resulting URL path
     */
    public function Zimm($instructionId, $imagePath, $customName = NULL, $customDir = NULL)
    {
        static $imgm = NULL;
        if (!$imgm) {
            $imgm = new Zimm_Model_DbTable_Images();
        }

        $imageId = $imgm->fetchImageId($imagePath);
        $extension = Zimm_Model_DbTable_Instructions::getInstructionExtension($instructionId);

        if (!$customName) {
            $customName = pathinfo($imagePath, PATHINFO_FILENAME);
        }

        $router = Zend_Controller_Front::getInstance()->getRouter();
        return $router->assemble(array(
                'i2iId'=>"$imageId.$instructionId",
                'imageId'=>$imageId,
                'instructionId'=>$instructionId,
                'dirname'=>$customDir,
                'basename'=>$customName . '.' . $extension,
                'filename'=>$customName,
                'extension'=>$extension,
            ), 'zimm', true);
    }
}